package com.yocard.yxyd.ui.fragment.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.base.BasePresenter;
import com.yocard.yxyd.base.BaseView;
import com.yocard.yxyd.di.component.DaggerFragmentComponent;
import com.yocard.yxyd.di.component.FragmentComponent;
import com.yocard.yxyd.di.module.FragmentModule;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.utils.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import static com.yocard.yxyd.event.EventCode.REFRESH_TOKEN;

public abstract class BaseBackFragment<T extends BasePresenter> extends SimpleFragment implements BaseView {

    @Inject
    protected T mPresenter;

    protected FragmentComponent getFragmentComponent(){
        return DaggerFragmentComponent.builder()
                .appComponent(AppContext.getAppComponent())
                .fragmentModule(getFragmentModule())
                .build();
    }
    private BackHandleInterface backHandleInterface;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getActivity() instanceof BackHandleInterface){
            this.backHandleInterface = (BackHandleInterface)getActivity();
        }else{
            throw new ClassCastException("Hosting Activity must implement BackHandledInterface");
        }
    }

    protected FragmentModule getFragmentModule(){
        return new FragmentModule(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        initInject();
        mPresenter.attachView(this);
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        backHandleInterface.onSelectedFragment(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        backHandleInterface.onSelectedFragment(null);
    }

    @Override
    public void onDestroyView() {
        if (mPresenter != null) mPresenter.detachView();
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Override
    public void showErrorMsg(String msg) {
        dismisDialog();
        ToastUtils.showLong(msg);
    }

    @Override
    public void refreshTokenSucceed() {
        dismisDialog();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFreshEvent(CommonBean bean) {
        if (bean.getCode() == REFRESH_TOKEN) {
            if (mPresenter != null)
                mPresenter.refreshToken();
        }
    }

    @Nullable
    @Override
    public Context getContext() {
        return mContext;
    }

    protected abstract void initInject();

    public abstract boolean onBackPressed();

    public static interface BackHandleInterface {

        void onSelectedFragment(BaseBackFragment fragment);

    }
}